/*
 * @Author: your name
 * @Date: 2021-04-02 15:33:13
 * @LastEditTime: 2021-05-31 16:15:37
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \egg-demo\app\controller\users.js
 */
'use strict';

const Controller = require('egg').Controller;

class UserController extends Controller {
  async info() {
    const { ctx } = this;
    ctx.body = '你好啊！~';
  }

  async list() {
    const { ctx } = this;
    const query = { limit: null, offset: null };
    const authorList = await ctx.model.Users.findAll(query);
    ctx.body = authorList;
    // if(authorList) {
    //   ctx.body = authorList;
    // } else {
    //   ctx.body = 'authorList is empty';
    // }

  }

  async getUser() {
    const { ctx } = this;
    ctx.body = ctx.params;
  }

  async create() {
    const { ctx } = this;
    const { name, age, idName:id_name } = ctx.request.body;
    const user = await ctx.model.Users.create({ name, age, id_name });
    // const user = await ctx.service.user.createItem(this);
    ctx.status = 201;
    ctx.body = user;
  }

  /* 测试sequelize模型查询 */
  async get_model_list() {
    const { ctx } = this;
    const query = { 
      limit: null, 
      ofset: null,
      attributes: {
        // 添加聚合
        // include: [
        //   sequelize.fn('COUNT', sequelize.col('gender')), 'n_gender'
        // ]
        // 排除某些属性
        exclude: [ 'updated_at' ]
      },
      where: {
        gender: 'F'
      }
    };
    
    const aimList = await ctx.model.Users.findAll(query);
    ctx.body = aimList;
  }

}

module.exports = UserController;
